package com.mingle.widget;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.multimodalinput.event.KeyEvent;

/**
 * Created by zzz40500 on 15/6/15.
 */
public class ShapeLoadingDialog extends CommonDialog implements BaseDialog.DialogListener, Component.KeyEventListener {

    private LoadingView mLoadingView;
    private Builder mBuilder;

    private ShapeLoadingDialog(Builder builder) {
        super(builder.mContext);
        mBuilder = builder;
        setDialogListener(this);
        setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        setAlignment(LayoutAlignment.CENTER);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        Component component = LayoutScatter.getInstance(mBuilder.mContext).parse(ResourceTable.Layout_layout_dialog, null, false);
        component.setTouchFocusable(true);
        component.requestFocus();
        component.setKeyEventListener(this);
        setContentCustomComponent(component);
        mLoadingView = (LoadingView) component.findComponentById(ResourceTable.Id_loadView);
        mLoadingView.setDelay(mBuilder.mDelay);
        mLoadingView.setLoadingText(mBuilder.mLoadText);
    }

    @Override
    public boolean isTouchOutside() {
        if (mBuilder.mCanceledOnTouchOutside) {
            ShapeLoadingDialog.this.destroy();
            return true;
        }
        return false;
    }

    @Override
    public boolean onKeyEvent(Component component, KeyEvent keyEvent) {
        if (keyEvent.getKeyCode() == KeyEvent.KEY_BACK && mBuilder.mCancelable) {
            ShapeLoadingDialog.this.destroy();
        }
        return true;
    }

    @Override
    public void show() {
        super.show();
        mLoadingView.setVisibility(Component.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        mLoadingView.setVisibility(Component.HIDE);
        super.onDestroy();
    }

    /**
     * 获取此Loading弹窗的构造器
     *
     * @return Loading弹窗构造器
     */
    public Builder getBuilder() {
        return mBuilder;
    }

    /**
     * Loading弹窗构造器
     */
    public static class Builder {

        private Context mContext;

        private int mDelay = 80;

        private String mLoadText;

        private boolean mCancelable = true;

        private boolean mCanceledOnTouchOutside = true;

        public Builder(Context context) {
            mContext = context;
        }

        /**
         * 设置loading控件延迟显示的时间
         *
         * @param delay loading控件延迟显示的时间
         * @return loading弹窗构造器自身
         */
        public Builder delay(int delay) {
            mDelay = delay;
            return this;
        }

        /**
         * 设置loading控件的文本
         *
         * @param loadText loading控件的文本
         * @return loading弹窗构造器自身
         */
        public Builder loadText(String loadText) {
            mLoadText = loadText;
            return this;
        }

        /**
         * 设置loading控件的文本资源id
         *
         * @param resId loading控件的文本资源id
         * @return loading弹窗构造器自身
         */
        public Builder loadText(int resId) {
            mLoadText = mContext.getString(resId);
            return this;
        }

        /**
         * 设置点击返回键时是否删除loading弹框
         *
         * @param cancelable 是否删除loading弹框
         * @return loading弹窗构造器自身
         */
        public Builder cancelable(boolean cancelable) {
            mCancelable = cancelable;
            mCanceledOnTouchOutside = cancelable;
            return this;
        }

        /**
         * 设置点击弹窗外部时是否删除loading弹框
         *
         * @param canceledOnTouchOutside 是否删除loading弹框
         * @return loading弹窗构造器自身
         */
        public Builder canceledOnTouchOutside(boolean canceledOnTouchOutside) {
            mCanceledOnTouchOutside = canceledOnTouchOutside;
            return this;
        }

        /**
         * 创建loading弹框
         *
         * @return loading弹框
         */
        public ShapeLoadingDialog build() {
            return new ShapeLoadingDialog(this);
        }

        /**
         * 创建并显示loading弹框
         *
         * @return loading弹框
         */
        public ShapeLoadingDialog show() {
            ShapeLoadingDialog dialog = build();
            dialog.show();
            return dialog;
        }
    }

}
